//
//  LoginByMobileView.m
//  YunDongGym
//
//  Created by lqq on 2018/6/12.
//  Copyright © 2018年 lqq. All rights reserved.
//

#import "LoginByMobileView.h"
#import "I_SendSms.h"
#import "I_Account.h"

@interface LoginByMobileView ()<UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UIView *mobileBackView;
@property (weak, nonatomic) IBOutlet UIView *codeBackView;
@property (weak, nonatomic) IBOutlet UITextField *mobileTextField;
@property (weak, nonatomic) IBOutlet UITextField *codeTextField;
@property (weak, nonatomic) IBOutlet UIButton *getCodeBtn;
@property (weak, nonatomic) IBOutlet UIButton *loginBtn;
@property (nonatomic, strong) NSTimer *timer;
@property (nonatomic, assign) int timerFireSeconds;//定时器开始了多少秒
@end


@implementation LoginByMobileView
-(void)dealloc
{
    NSLog(@"%s",__func__);
    [_timer invalidate];
    _timer = nil;
}

-(void)awakeFromNib
{
    [super awakeFromNib];
    
    [_mobileTextField setAttributedPlaceholder:[[NSAttributedString alloc] initWithString:@"手机号" attributes:@{NSForegroundColorAttributeName:HEXColor(@"4A4A4A")}]];
    
    [_codeTextField setAttributedPlaceholder:[[NSAttributedString alloc] initWithString:@"验证码" attributes:@{NSForegroundColorAttributeName:HEXColor(@"4A4A4A")}]];
    
    
    NSArray *needCornerRadiusViews = @[_mobileBackView,_codeBackView,_loginBtn];
    for (UIView *view in needCornerRadiusViews) {
        view.layer.cornerRadius = 2;
        view.layer.masksToBounds = YES;
    }
    
    [_mobileTextField addTarget:self action:@selector(textFieldChanged:) forControlEvents:UIControlEventEditingChanged];
    [_codeTextField addTarget:self action:@selector(textFieldChanged:) forControlEvents:UIControlEventEditingChanged];
    
    _mobileTextField.delegate = self;
    _mobileTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
    _codeTextField.delegate = self;
    
    //登录按钮设置
    [_loginBtn setBackgroundImage:[UIImage imageWithColor:[UIColor colorWithHexString:@"3AA7FF"] size:CGSizeMake(1, 1)] forState:UIControlStateNormal];
    [_loginBtn setBackgroundImage:[UIImage imageWithColor:[UIColor colorWithHexString:@"DDDDDD"] size:CGSizeMake(1, 1)] forState:UIControlStateDisabled];
    _loginBtn.enabled = NO;
    
    //获取验证码按钮设置
    [_getCodeBtn setTitleColor:HEXColor(@"3AA7FF") forState:UIControlStateNormal];
    [_getCodeBtn setTitleColor:HEXColor(@"CCCCCC") forState:UIControlStateDisabled];
    _getCodeBtn.enabled = NO;
}





/**
 获取验证码按钮
 */
- (IBAction)getVarifyCode:(id)sender {
    __weak __typeof (self)weakSelf = self;
    _getCodeBtn.enabled = NO;
    [I_SendSms sendSMSToMobile:_mobileTextField.text smsType:SendSMSType_Login success:^{
        //开启定时器
        [weakSelf.timer invalidate];
        weakSelf.timer = nil;
        weakSelf.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:weakSelf selector:@selector(countDown) userInfo:nil repeats:YES];
        [[NSRunLoop currentRunLoop] addTimer:weakSelf.timer forMode:NSRunLoopCommonModes];
        weakSelf.timerFireSeconds = 1;
    } failure:^(NSError *error) {
        weakSelf.getCodeBtn.enabled = YES;
        [AV showError:error];
    }];
    
}


- (IBAction)loginBtnClicked:(id)sender {
    [self endEditing:YES];
    __weak __typeof (self)weakSelf = self;
    NSString *mobile = _mobileTextField.text;
    
    [AV showLoading:@"正在登陆"];
    [I_Account smsLoginWithMobile:mobile code:_codeTextField.text success:^(M_Account *account) {
         [AV hideen];
        [weakSelf loginSuccessWithAccount:account];
    } failure:^(NSError *error) {
        if (weakSelf.loginErrorBlock) {
            weakSelf.loginErrorBlock(error);
        }
    }];
}
- (IBAction)changeToAccountLogin:(id)sender {
    if (_changeToAccountBlock) {
        _changeToAccountBlock();
    }
}

#pragma mark - Pravite
/**
 倒计时
 */
- (void)countDown
{
    if (_timerFireSeconds == 60) {
        [_timer invalidate];
        _timer = nil;
        [_getCodeBtn setTitle:@"获取验证码" forState:UIControlStateDisabled];
        if (_mobileTextField.text.length >= 11) {
            _getCodeBtn.enabled = YES;
        }
    } else {
        _getCodeBtn.enabled = NO;
        [_getCodeBtn setTitle:[NSString stringWithFormat:@"重新获取(%d)",(60-_timerFireSeconds)] forState:UIControlStateDisabled];
        _timerFireSeconds += 1;
    }
}

- (void)loginSuccessWithAccount:(M_Account *)account
{
    
    [self.timer invalidate];
    self.timer = nil;
    if (_loginSuccessBlock) {
        _loginSuccessBlock(account);
    }

    
}

#pragma mark - KVO
- (void)textFieldChanged:(UITextField *)textField
{
    if (textField == _codeTextField  && [_codeTextField isFirstResponder]) {//是否是第一响应者
        if (_codeTextField.text.length > 4) {
            _codeTextField.text = [_codeTextField.text substringToIndex:4];
        }
    } else if (textField == _mobileTextField) {
        if (_mobileTextField.text.length >= 11) {
            _mobileTextField.text = [_mobileTextField.text substringToIndex:11];
            _getCodeBtn.enabled = YES;
        } else {
            _getCodeBtn.enabled = NO;
        }
    }
    if (_codeTextField.text.length >= 4 && _mobileTextField.text.length >= 11) {
        _loginBtn.enabled = YES;
    } else {
        _loginBtn.enabled = NO;
    }
    
}
#pragma mark - UITextFiledDelegate
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    if (textField == _mobileTextField) {
    } else if (textField == _codeTextField) {
    }
    return YES;
}


@end
